<template>
  <div style="margin-top: 16px">
    <a-form-item label="处理人规则">
      <a-select v-model="userTaskForm.taskRules1" @change="updateElementTask('taskRules1')">
        <a-select-option v-for="(trs, index) in taskRules1" :key="index" :label="trs.taskRuleName"
          :value="trs.ruleKey" />
      </a-select>
      <div v-if="processTaskRule == 'byUser'">
        <a-table :data="assigneeUserList" size="mini" max-height="240" border fit>
          <a-table-column label="序号" width="50px" type="index" />
          <a-table-column label="帐号" prop="username" show-overflow-tooltip />
          <a-table-column label="姓名" prop="name" show-overflow-tooltip />
          <a-table-column label="操作" fixed="right" width="90px">
            <template slot-scope="{ row, $index }">
              <a-button size="mini" type="text" style="color: #ff4d4f" @click="removeUser(row, $index)">移除</a-button>
            </template>
          </a-table-column>
        </a-table>
        <div class="element-drawer__button">
          <a-button size="mini" type="primary" icon="a-icon-plus"
            @click="openUserbutesForm(null, -1, 'byUser')">添加人员</a-button>
        </div>
      </div>
      <a-dialog :visible.sync="byUserFormModelVisible" :close-on-click-modal="false" title="选择人员" width="80%"
        append-to-body destroy-on-close>
        <a-form :model="byUserForm" label-width="80px" style="width: 99%" size="mini">
          <ruleToUser ref="assigneeByUserRef"></ruleToUser>
        </a-form>
        <template slot="footer">
          <a-button size="mini" @click="byUserFormModelVisible = false">取 消</a-button>
          <a-button size="mini" type="primary" @click="saveAssigneeByUser('byUser')">确 定</a-button>
        </template>
      </a-dialog>
      <div v-if="processTaskRule == 'byDept'">
        <a-table :data="assigneeUserList" size="mini" max-height="240" border fit>
          <a-table-column label="序号" width="50px" type="index" />
          <a-table-column label="部门名称" prop="name" show-overflow-tooltip />
          <a-table-column label="操作" fixed="right" width="90px">
            <template slot-scope="{ row, $index }">
              <a-button size="mini" type="text" style="color: #ff4d4f" @click="removeUser(row, $index)">移除</a-button>
            </template>
          </a-table-column>
        </a-table>
        <div class="element-drawer__button">
          <a-button size="mini" type="primary" icon="a-icon-plus"
            @click="openUserbutesForm(null, -1, 'byDept')">添加部门</a-button>
        </div>
      </div>
      <a-dialog :visible.sync="byDeptFormModelVisible" :close-on-click-modal="false" title="选择部门" width="80%"
        append-to-body destroy-on-close>
        <a-form :model="byDeptForm" label-width="80px" style="width: 99%" size="mini">
          <ruleToUser ref="assigneeByDeptRef"></ruleToUser>
        </a-form>
        <template slot="footer">
          <a-button size="mini" @click="byDeptFormModelVisible = false">取 消</a-button>
          <a-button size="mini" type="primary" @click="saveAssigneeByUser('byDept')">确 定</a-button>
        </template>
      </a-dialog>
      <div v-if="processTaskRule == 'byRole'">
        <template>
          <a-select v-model="userTaskForm.assigneeRoles" multiple placeholder="请选择角色">
            <a-select-option v-for="item in assigneeRoles" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus" @click="saveAssigneeByUser('byRole')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byPosition'">
        <template>
          <a-select v-model="userTaskForm.assigneePosition" multiple placeholder="请选择岗位">
            <a-select-option v-for="item in assigneePosition" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byPosition')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byGroup'">
        <template>
          <a-select v-model="userTaskForm.assigneeGroup" multiple placeholder="请选择分组">
            <a-select-option v-for="item in groupList" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus" @click="saveAssigneeByUser('byGroup')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byDeptAndPosition'">
        <template>
          <a-tree-select class="tree" v-model="userTaskForm.mixedDept" :tree-data="deptList" :multiple="true"
            placeholder="请选择部门">
          </a-tree-select>
          <a-select v-model="userTaskForm.mixedPosition" multiple placeholder="请选择岗位">
            <a-select-option v-for="item in assigneePosition" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byDeptAndPosition')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byDeptAndRole'">
        <template>
          <a-tree-select class="tree" v-model="userTaskForm.mixedDept" :data="deptList" placeholder="请选择部门">
          </a-tree-select>
          <a-select v-model="userTaskForm.mixedRole" multiple placeholder="请选择角色">
            <a-select-option v-for="item in assigneeRoles" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byDeptAndRole')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byTaskManager'">
        <template>
          <a-select v-model="userTaskForm.byTaskManager" placeholder="请选择节点">
            <a-select-option v-for="item in taskList" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byTaskManager')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byTaskLeader'">
        <template>
          <a-select v-model="userTaskForm.byTaskLeader" placeholder="请选择节点">
            <a-select-option v-for="item in taskList" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byTaskLeader')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byNode'">
        <template>
          <a-select v-model="userTaskForm.byNode" placeholder="请选择节点">
            <a-select-option v-for="item in taskList" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus" @click="saveAssigneeByUser('byNode')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'bySql'">
        <template>
          <a-input v-model="userTaskForm.bySql" clearable />
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus" @click="saveAssigneeByUser('bySql')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byWebAPI'">
        <template>
          <a-input v-model="userTaskForm.byWebAPI" clearable />
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byWebAPI')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byParams'">
        <template>
          <a-input v-model="userTaskForm.byParams" clearable />
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byParams')">确认</a-button>
          </div>
        </template>
      </div>
      <div v-if="processTaskRule == 'byManuallySelect'">
        <template>
          <a-select v-model="userTaskForm.byManuallySelect" placeholder="设置范围" clearable>
            <a-select-option value="allUser" label="全部人员" />
            <a-select-option value="byManuallySelectDept" label="按部门" />
            <a-select-option value="byManuallySelectRole" label="按角色" />
            <a-select-option value="byManuallySelectStion" label="按岗位" />
          </a-select>
          <a-tree-select v-if="userTaskForm.byManuallySelect == 'byManuallySelectDept'" class="tree"
            v-model="userTaskForm.bmsDept" :data="deptList" placeholder="请选择部门">
          </a-tree-select>
          <a-select v-if="userTaskForm.byManuallySelect == 'byManuallySelectRole'" v-model="userTaskForm.bmsRole" multiple
            placeholder="请选择角色">
            <a-select-option v-for="item in assigneeRoles" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <a-select v-if="userTaskForm.byManuallySelect == 'byManuallySelectStion'" v-model="userTaskForm.bmsPosition"
            multiple placeholder="请选择岗位">
            <a-select-option v-for="item in assigneePosition" :key="item.id" :label="item.name" :value="item.id">
            </a-select-option>
          </a-select>
          <div class="element-drawer__button">
            <a-button size="mini" type="primary" icon="a-icon-plus"
              @click="saveAssigneeByUser('byManuallySelect')">确认</a-button>
          </div>
        </template>
      </div>
    </a-form-item>
    <a-form-item label="设置说明:" prop="overDueWarUrl">
      <span style="color: darkorange">设置或修改处理人规则后，需要保存发布才可生效</span>
    </a-form-item>
  </div>
</template>

<script lang="ts" setup>
import { mapGetters } from 'vuex';
import ruleToUser from '@b/components/taskcomponents/ruleToUser.vue';
import ruleToDept from '@b/components/taskcomponents/ruleToDept.vue';
import { useTaskRuleStore } from '@/store'
import { storeToRefs } from 'pinia'
import { getOrgData } from '@bs/orgm/orgm';
import {
  setAssigneeUserWithTaskId,
  getAssigneeUserWithTaskId,
  deleteAssigneeUser,
  setFlowAttrs
} from '@bs/process/process';
defineOption({ name: 'UserTask' })
const props = defineProps<{
  id: string,
  type: string,
  element: object,
}>()

const { taskRules, startRules } = storeToRefs(useTaskRuleStore())

const defaultTaskForm = reactive({
  taskRules1: '',
  assignee: '',
  assigneeRoles: '',
  assigneePosition: '',
  candidateUsers: [],
  candidateGroups: [],
  dueDate: '',
  priority: ''
})
const userTaskForm = reactive({
  taskRules1: '',
  assignee: '',
  assigneeRoles: '',
  assigneePosition: '',
  assigneeGroup: '',
  byTaskManager: '',
  byTaskLeader: '',
  byNode: '',
  bySql: '',
  byWebAPI: '',
  byParams: '',
  byManuallySelect: '',
  mixedDept: null,
  mixedPosition: '',
  mixedRole: '',
  bmsDept: null,
  bmsPosition: '',
  bmsRole: '',
  bmsAllUser: '',
  candidateUsers: [],
  candidateGroups: [],
  dueDate: '',
  priority: ''
})
const assigneeUserList = ref([])
const taskRules1 = ref([])
const deptList = ref([])
const groupList = ref([])
const taskList = ref([])
const processTaskRule = ref('')
const processReturnWay = ref('')
const processCCWay = ref('')
const userList = ref([])
const assigneeRoles = ref([])
const assigneePosition = ref([])
const byUserForm = ref({})
const byDeptForm = ref({})
const byUserFormModelVisible = ref(false)
const byDeptFormModelVisible = ref(false)
const editingCCWayIndex = ref(-1)
const autoCCWayVisible = ref(false)
const mockData = ref([1, 2, 3, 4, 5, 6, 7])

onMounted(() => {
  taskLoad();
})

watch(() => props.id, (nval, oval) => {
  nextTick(() => {
    resetTaskForm()
  })
})

async function taskLoad() {
  let taskType = '';
  if (this.element.businessObject.$type == 'bpmn:StartEvent') {
    taskType = 'bpmn:StartEvent';
  } else {
    const taskInComing = this.element.businessObject.incoming;
    const taskSourceRef = taskInComing[0].sourceRef;
    taskType = taskSourceRef.$type;
  }
  if (taskType == 'bpmn:StartEvent') {
    this.taskRules1 = this.getStartRules;
  } else {
    this.taskRules1 = this.getTaskRules;
  }
  let tasks = this.element.children;
  if (this.element.type == "bpmn:UserTask") {
    tasks = this.element.parent.children;
  }
  this.taskList = [];
  let taskElement = tasks.filter((item) => {
    if (item.businessObject.$type == "bpmn:UserTask") {
      let element = {
        id: item.businessObject.id,
        name: item.businessObject.name
      };
      if (this.element.id != item.businessObject.id) {
        this.taskList.push(element);
      }
      return item;
    }
  });
  const res = await getOrgData();
  if (res.error == '200') {
    this.assigneeRoles = res.result.rolesList;
    this.deptList = res.result.departList;
    this.assigneePosition = res.result.positionList;
    this.groupList = res.result.groupEntityList;
  }
}

function resetTaskForm() {
  this.taskLoad();
  for (let key in this.defaultTaskForm) {
    let value;
    if (key === 'candidateUsers') {
      value = this.element?.businessObject[key]
        ? this.element.businessObject[key].split(',')
        : [];
      //如果是usertask，将他的规则类型取出来
      if (value == undefined || value == '' || value == null) {
        value = [];
        this.processTaskRule = '';
      } else {
        let ruleRep = value[0].replace(this.id + '_', '');
        ruleRep = ruleRep.replace('$', '');
        ruleRep = ruleRep.replace('{', '');
        value = ruleRep.replace('}', '');
        this.processTaskRule = value;
        this.userTaskForm.taskRules1 = value;
        this.loadAssigneeUserList(value);
      }
    } else if (key === 'candidateGroups') {
      value = this.element?.businessObject[key]
        ? this.element.businessObject[key].split(',')
        : [];
    } else {
      value = this.element?.businessObject[key] || this.defaultTaskForm[key];
    }
    this.$set(this.userTaskForm, key, value);
  }
}

//规则选择
function updateElementTask(key) {
  const taskAttr = Object.create(null);
  if (key == 'taskRules1') {
    taskAttr['candidateUsers'] = '${' + this.id + '_' + this.userTaskForm[key] + '}';
    this.processTaskRule = this.userTaskForm[key];
    this.loadAssigneeUserList(this.processTaskRule);
  }
  window.bpmnInstances.modeling.updateProperties(this.element, taskAttr);
}

//根据指定用户规则，设置任务处理人
function saveAssigneeByUser(key) {
  let task_def_key = this.id + "_" + key;
  let selectData = null;
  if (key == 'byUser') {
    selectData = this.$refs.assigneeByUserRef.saveTaskUser();
  } else if (key == 'byDept') {
    selectData = this.$refs.assigneeByDeptRef.saveTaskUser();
  } else if (key == 'byRole') {
    if (this.userTaskForm.assigneeRoles == '') {
      this.$message.warning('请选择角色');
      return;
    }
    selectData = [];
    let byRoleObj = {
      id: '',
      name: ''
    };
    this.userTaskForm.assigneeRoles.forEach((element) => {
      byRoleObj = {
        id: element,
        name: ''
      };
      selectData.push(byRoleObj);
    });
  } else if (key == 'byPosition') {
    if (this.userTaskForm.assigneePosition == '') {
      this.$message.warning('请选择岗位');
      return;
    }
    selectData = [];
    let byPositionObj = {
      id: '',
      name: ''
    };
    this.userTaskForm.assigneePosition.forEach((element) => {
      byPositionObj = {
        id: element,
        name: ''
      };
      selectData.push(byPositionObj);
    });
  }
  else if (key == 'byTaskManager') {
    if (this.userTaskForm.byTaskManager == '') {
      this.$message.warning('请选择节点');
      return;
    }
    selectData = [];
    let byTaskManagerObj = {
      id: this.userTaskForm.byTaskManager,
      username: this.userTaskForm.byTaskManager
    };
    selectData.push(byTaskManagerObj);
  }
  else if (key == 'byTaskLeader') {
    if (this.userTaskForm.byTaskLeader == '') {
      this.$message.warning('请选择节点');
      return;
    }
    selectData = [];
    let byTaskLeaderObj = {
      id: this.userTaskForm.byTaskLeader,
      username: this.userTaskForm.byTaskLeader
    };
    selectData.push(byTaskLeaderObj);
  }
  else if (key == 'byNode') {
    if (this.userTaskForm.byNode == '') {
      this.$message.warning('请选择节点');
      return;
    }
    selectData = [];
    let bybyNodeObj = {
      id: this.userTaskForm.byNode,
      username: this.userTaskForm.byNode
    };
    selectData.push(bybyNodeObj);
  }
  else if (key == 'byGroup') {
    if (this.userTaskForm.assigneeGroup == '') {
      this.$message.warning('请选择分组');
      return;
    }
    selectData = [];
    let byGroupObj = {
      id: '',
      name: ''
    };
    this.userTaskForm.assigneeGroup.forEach((element) => {
      byGroupObj = {
        id: element,
        name: ''
      };
      selectData.push(byGroupObj);
    });
  }
  else if (key == 'byDeptAndPosition') {
    task_def_key = this.id + "_byDeptAndPosition";
    selectData = [];
    if (this.userTaskForm.mixedDept == null) {
      this.$message.warning('请选择部门');
      return;
    }
    if (this.userTaskForm.mixedPosition == '') {
      this.$message.warning('请选择岗位');
      return;
    }
    let byDeptAndPosition = {
      id: this.id,
      targetid: this.userTaskForm.mixedDept,
      dsr: 'dept'
    };
    selectData.push(byDeptAndPosition);
    this.userTaskForm.mixedPosition.forEach((element) => {
      byDeptAndPosition = {
        id: this.id,
        targetid: element,
        dsr: 'position'
      };
      selectData.push(byDeptAndPosition);
    });
  }
  else if (key == "byDeptAndRole") {
    task_def_key = this.id + "_byDeptAndRole";
    if (this.userTaskForm.mixedDept == null) {
      this.$message.warning('请选择部门');
      return;
    }
    if (this.userTaskForm.mixedRole == '') {
      this.$message.warning('请选择角色');
      return;
    }
    let byDeptAndRole = {
      id: this.id,
      targetid: this.userTaskForm.mixedDept,
      dsr: 'dept'
    };
    selectData = [];
    selectData.push(byDeptAndRole);
    this.userTaskForm.mixedRole.forEach((element) => {
      byDeptAndRole = {
        id: this.id,
        targetid: element,
        dsr: 'role'
      };
      selectData.push(byDeptAndRole);
    });
  }
  else if (key == 'bySql') {
    if (this.userTaskForm.bySql == '') {
      this.$message.warning('请填写SQL语句');
      return;
    }
    selectData = [];
    let bySqlObj = {
      username: this.userTaskForm.bySql,
      id: 'bySql'
    };
    selectData.push(bySqlObj);
  }
  else if (key == 'byWebAPI') {
    if (this.userTaskForm.byWebAPI == '') {
      this.$message.warning('请填写webAPI地址');
      return;
    }
    selectData = [];
    let byWebAPIObj = {
      username: this.userTaskForm.byWebAPI,
      id: 'byWebAPI'
    };
    selectData.push(byWebAPIObj);
  }
  else if (key == 'byParams') {
    if (this.userTaskForm.byParams == '') {
      this.$message.warning('请填写参数名称');
      return;
    }
    selectData = [];
    let byParamsObj = {
      username: this.userTaskForm.byParams,
      id: 'byParams'
    };
    selectData.push(byParamsObj);
  } else if (key == 'byManuallySelect') {
    task_def_key = this.id + "_byManuallySelect";
    selectData = [];
    let byManuallySelect = {
      id: this.id,
      targetid: this.userTaskForm.byManuallySelect,
      dsr: 'allUser'
    };
    if (this.userTaskForm.byManuallySelect == 'byManuallySelectDept') {
      if (this.userTaskForm.bmsDept == null) {
        this.$message.warning('请选择部门');
        return;
      }
      byManuallySelect = {
        id: this.id,
        targetid: this.userTaskForm.bmsDept,
        dsr: 'byManuallySelectDept'
      };
      selectData.push(byManuallySelect);
    }
    else if (this.userTaskForm.byManuallySelect == 'byManuallySelectRole') {
      if (this.userTaskForm.bmsRole == '') {
        this.$message.warning('请选择角色');
        return;
      }
      this.userTaskForm.bmsRole.forEach((element) => {
        byManuallySelect = {
          id: this.id,
          targetid: element,
          dsr: 'byManuallySelectRole'
        };
        selectData.push(byManuallySelect);
      });
    }
    else if (this.userTaskForm.byManuallySelect == 'byManuallySelectStion') {
      if (this.userTaskForm.bmsPosition == '') {
        this.$message.warning('请选择岗位');
        return;
      }
      this.userTaskForm.bmsPosition.forEach((element) => {
        byManuallySelect = {
          id: this.id,
          targetid: element,
          dsr: 'byManuallySelectStion'
        };
        selectData.push(byManuallySelect);
      });
    }
    else {
      selectData.push(byManuallySelect);
    }
  }
  setAssigneeUserWithTaskId({
    userlist: selectData,
    usertaskid: task_def_key,
    id: this.id,
    ruleName: key
  }).then((res) => {
    if (res.error == '200') {
      this.$message.success('设置成功');
      this.byUserFormModelVisible = false;
      this.byDeptFormModelVisible = false;
      this.loadAssigneeUserList(key);
    }
  });
}

//根据指定用户规则，新增任务处理人
function openUserbutesForm(attr, index, type) {
  this.editingPropertyIndex = index;
  this.propertyForm = index === -1 ? {} : JSON.parse(JSON.stringify(attr));
  if (type == 'byUser') {
    this.byUserFormModelVisible = true;
  } else if (type == 'byDept') {
    this.byDeptFormModelVisible = true;
  }
}

//根据指定用户规则，显示配置的任务处理人
function loadAssigneeUserList(key) {
  getAssigneeUserWithTaskId({ usertaskid: this.id, ruleName: key }).then((res) => {
    if (res.error == '200') {
      if (key == 'byRole') {
        var roleIntSet = [];
        for (let roleKey in res.result.assigneeUserEntityList) {
          if (res.result.assigneeUserEntityList[roleKey].username == "" ||
            res.result.assigneeUserEntityList[roleKey].username == undefined) {
            roleIntSet.push(res.result.assigneeUserEntityList[roleKey].id);
          }
          else {
            roleIntSet.push(res.result.assigneeUserEntityList[roleKey].username);
          }
        }
        this.userTaskForm.assigneeRoles = roleIntSet;
      } else if (key == 'byPosition') {
        var postIntSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          if (res.result.assigneeUserEntityList[ptKey].username == "" ||
            res.result.assigneeUserEntityList[ptKey].username == undefined) {
            postIntSet.push(res.result.assigneeUserEntityList[ptKey].id);
          }
          else {
            postIntSet.push(res.result.assigneeUserEntityList[ptKey].username);
          }
        }
        this.userTaskForm.assigneePosition = postIntSet;
      } else if (key == 'byGroup') {
        var groupIntSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          if (res.result.assigneeUserEntityList[ptKey].username == "" ||
            res.result.assigneeUserEntityList[ptKey].username == undefined) {
            groupIntSet.push(res.result.assigneeUserEntityList[ptKey].id);
          }
          else {
            groupIntSet.push(res.result.assigneeUserEntityList[ptKey].username);
          }
        }
        this.userTaskForm.assigneeGroup = groupIntSet;
      } else if (key == 'byManuallySelect') {
        var bmsDeptIntSet = 0;
        var bmsRoleIntSet = [];
        var bmsAllUserIntSet = "";
        var bmsStionIntSet = [];
        for (let dpKey in res.result.assigneeDSREntities) {
          if (res.result.assigneeDSREntities[dpKey].dsr == "byManuallySelectDept") {
            bmsDeptIntSet = res.result.assigneeDSREntities[dpKey].targetId;
            this.userTaskForm.byManuallySelect = "byManuallySelectDept";
          }
          if (res.result.assigneeDSREntities[dpKey].dsr == "byManuallySelectRole") {
            bmsRoleIntSet.push(res.result.assigneeDSREntities[dpKey].targetId);
            this.userTaskForm.byManuallySelect = "byManuallySelectRole";
          }
          if (res.result.assigneeDSREntities[dpKey].dsr == "allUser") {
            bmsAllUserIntSet = res.result.assigneeDSREntities[dpKey].targetId;
            this.userTaskForm.byManuallySelect = "allUser";
          }
          if (res.result.assigneeDSREntities[dpKey].dsr == "byManuallySelectStion") {
            bmsStionIntSet.push(res.result.assigneeDSREntities[dpKey].targetId);
            this.userTaskForm.byManuallySelect = "byManuallySelectStion";
          }
        }
        this.userTaskForm.bmsDept = bmsDeptIntSet[0];
        this.userTaskForm.bmsRole = bmsRoleIntSet;
        this.userTaskForm.bmsPosition = bmsStionIntSet;
      }
      else if (key == 'byDeptAndPosition') {
        var deptIntSet = 0;
        var postIntSet = [];
        for (let dpKey in res.result.assigneeDSREntities) {
          if (res.result.assigneeDSREntities[dpKey].dsr == "dept") {
            deptIntSet = res.result.assigneeDSREntities[dpKey].targetId;
          }
          if (res.result.assigneeDSREntities[dpKey].dsr == "position") {
            postIntSet.push(res.result.assigneeDSREntities[dpKey].targetId);
          }
        }

        this.userTaskForm.mixedDept = Number(deptIntSet);
        this.userTaskForm.mixedPosition = postIntSet;
      } else if (key == 'byDeptAndRole') {
        var deptIntSet = 0;
        var roleIntSet = [];
        for (let drKey in res.result.assigneeDSREntities) {
          if (res.result.assigneeDSREntities[drKey].dsr == "dept") {
            deptIntSet = res.result.assigneeDSREntities[drKey].targetId;
          }
          if (res.result.assigneeDSREntities[drKey].dsr == "role") {
            roleIntSet.push(res.result.assigneeDSREntities[drKey].targetId);
          }
        }
        this.userTaskForm.mixedDept = Number(deptIntSet);
        this.userTaskForm.mixedRole = roleIntSet;
      }
      else if (key == 'byTaskManager') {
        var managerSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          managerSet.push(res.result.assigneeUserEntityList[ptKey].username);
        }
        this.userTaskForm.byTaskManager = managerSet[0];
      }
      else if (key == 'byTaskLeader') {
        var leaderSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          leaderSet.push(res.result.assigneeUserEntityList[ptKey].username);
        }
        this.userTaskForm.byTaskLeader = leaderSet[0];
      }
      else if (key == 'bySql') {
        var bySqltSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          bySqltSet.push(res.result.assigneeUserEntityList[ptKey].username);
        }
        this.userTaskForm.bySql = bySqltSet[0];
      }
      else if (key == 'byParams') {
        var byParamsSet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          byParamsSet.push(res.result.assigneeUserEntityList[ptKey].username);
        }
        this.userTaskForm.byParams = byParamsSet[0];
      }
      else if (key == 'byWebAPI') {
        var byWebAPISet = [];
        for (let ptKey in res.result.assigneeUserEntityList) {
          byWebAPISet.push(res.result.assigneeUserEntityList[ptKey].username);
        }
        this.userTaskForm.byWebAPI = byWebAPISet[0];
      } else {
        this.assigneeUserList = res.result.assigneeUserEntityList;
      }
    }
  });
}

//移除节点处理人
function removeUser(row, index) {
  deleteAssigneeUser({ usertaskid: this.id, username: row.username }).then((res) => {
    if (res.error == '200') {
      this.$message.success('移除成功');
      this.assigneeUserList.splice(index, 1);
    }
  });
}

//定义参数
function setFlowElementAttrs(redisKey, attrKey) {
  setFlowAttrs({ redisKey: redisKey, attrKey: attrKey }).then((res) => { });
}

function openAutoCCWayForm(listener, index) {
  if (listener) {
    this.editingCCWayIndex = index;
  } else {
    this.editingCCWayIndex = -1; // 标记为新增
  }
  // 打开侧边栏并清楚验证状态
  this.autoCCWayVisible = true;
}

</script>
